package my.com.baidu.rigel.pss.wbdp.pojo.accstruct;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.builder.ToStringBuilder;

/**
 * 创意信息
 * 
 * @author fanhongjie
 * 
 */
public class Idea implements Serializable {

	private static final long serialVersionUID = -8611828414783530124L;
	/**
	 * 
	 */
	// private static final Pattern PATTERN = Pattern.compile("\\{.{1,}?\\}");
	/** 创意ID */
	private long id;
	/** 计划ID */
	private long planID;
	/** 单元ID */
	private long unitID;
	/** 创意标题 */
	private String title;
	/** 描述1 */
	private String desc1;
	/** 描述2 */
	private String desc2;
	/** 访问url */
	private String actUrl;
	/** 显示url */
	private String showUrl;
	/** 启用暂定 false启用 true暂定 */
	private boolean pause;
	/** 创意状态 51-有效 52-暂停推广 53-不宜推广 54-待激活 55-审核中 */
	private Integer status;
	/** 默认关键词index和word的对应 */
	private Map<Integer, Word> words;
	/** 意愿类型 核心 商机 拓展 */
	private int desireType;
	/** 二级行业ID */
	private String industry2Name;
	/** 一级业务ID */
	private String biz1Name;
	/** 需求类型 */
	private String requireType;
	/** 词性 */
	private String wordType;
	/** 创意来源：1 通过创意默认关键词推荐 2 通过老词推荐 */
	private int source;
	/** 是否已产生默认关键词 */
	private boolean hasGenerated;
	/** 创意内容 由title desc1 desc2去掉创意默认关键词后组成 */
	private String content;

	public Idea() {
		this.id = AccConstant.ACC_DEFAULT_LONG;
		this.planID = AccConstant.ACC_DEFAULT_LONG;
		this.unitID = AccConstant.ACC_DEFAULT_LONG;
		this.title = AccConstant.ACC_DEFAULT_STRING;
		this.desc1 = AccConstant.ACC_DEFAULT_STRING;
		this.desc2 = AccConstant.ACC_DEFAULT_STRING;
		this.actUrl = AccConstant.ACC_DEFAULT_STRING;
		this.showUrl = AccConstant.ACC_DEFAULT_STRING;
		this.words = new HashMap<Integer, Word>(
				AccConstant.ACC_DEFAULT_COLLECTION_SIZE);
		this.desireType = AccConstant.DESIRE_DEFAULT;
		this.industry2Name = AccConstant.ACC_DEFAULT_STRING;
		this.biz1Name = AccConstant.ACC_DEFAULT_STRING;
		this.requireType = AccConstant.ACC_DEFAULT_STRING;
		this.wordType = AccConstant.ACC_DEFAULT_STRING;
		this.source = AccConstant.IDEA_SOURCE_DEFAULT;
		this.hasGenerated = false;
		this.content = AccConstant.ACC_DEFAULT_STRING;
		this.status = AccConstant.IDEA_STATUS_DEFAULT;
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public long getPlanID() {
		return planID;
	}

	public void setPlanID(long planID) {
		this.planID = planID;
	}

	public long getUnitID() {
		return unitID;
	}

	public void setUnitID(long unitID) {
		this.unitID = unitID;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getDesc1() {
		return desc1;
	}

	public void setDesc1(String desc1) {
		this.desc1 = desc1;
	}

	public String getDesc2() {
		return desc2;
	}

	public void setDesc2(String desc2) {
		this.desc2 = desc2;
	}

	public String getActUrl() {
		return actUrl;
	}

	public void setActUrl(String actUrl) {
		this.actUrl = actUrl;
	}

	public String getShowUrl() {
		return showUrl;
	}

	public Integer getStatus() {
		return status;
	}

	public void setStatus(Integer status) {
		this.status = status;
	}

	public void setShowUrl(String showUrl) {
		this.showUrl = showUrl;
	}

	public Map<Integer, Word> getWords() {
		this.generateKeyWord();
		return words;
	}

	public void setWords(Map<Integer, Word> words) {
		this.words = words;
	}

	public int getDesireType() {
		return desireType;
	}

	public void setDesireType(int desireType) {
		this.desireType = desireType;
	}

	public String getIndustry2Name() {
		return industry2Name;
	}

	public void setIndustry2Name(String industry2Name) {
		this.industry2Name = industry2Name;
	}

	public String getBiz1Name() {
		return biz1Name;
	}

	public void setBiz1Name(String biz1Name) {
		this.biz1Name = biz1Name;
	}

	public String getRequireType() {
		return requireType;
	}

	public void setRequireType(String requireType) {
		this.requireType = requireType;
	}

	public String getWordType() {
		return wordType;
	}

	public void setWordType(String wordType) {
		this.wordType = wordType;
	}

	public int getSource() {
		return source;
	}

	public void setSource(int source) {
		this.source = source;
	}

	public boolean getPause() {
		return pause;
	}

	public void setPause(boolean pause) {
		this.pause = pause;
	}

	public String getContent() {
		if (!hasGenerated) {
			this.generateKeyWord();
		}
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	/**
	 * 产生默认关键词和索引位置的对应关系，并去除title desc1 desc2中得默认关键词以及{}
	 * 匹配规则是从第一个“{”起，到第一个“}”止的内容 忽略{}连在一起的情况
	 * 
	 */
	public void generateKeyWord() {
		if (!hasGenerated) {
			StringBuilder sb = new StringBuilder();

			sb.append(this.generateKeyWord(title,
					sb.length() + this.words.size()));
			sb.append(this.generateKeyWord(this.desc1,
					sb.length() + this.words.size()));
			sb.append(this.generateKeyWord(this.desc2,
					sb.length() + this.words.size()));
			content = sb.toString();
			//System.out.println(content);
			this.hasGenerated = true;
		}
	}

	public String generateKeyWord(String str, int addIndex) {
		StringBuilder sb = new StringBuilder();
		char[] chars = str.toCharArray();
		// 符号{开始处，start为-1时说明尚未找到{
		int start = -1;
		// 上一次}的index
		int last = -1;
		int position = 0;
		for (int index = 0; index < chars.length; index++) {
			if (chars[index] == '{' && start == -1) {
				start = index;
			}
			if (start == -1) {
				position++;
			}
			if (chars[index] == '}' && start != -1) {
				if (start != index - 1) {
					String value = str.substring(start + 1, index);
					Word word = new Word();
					word.setName(value);
					this.words.put(position + addIndex, word);
					sb.append(str.substring(last + 1, start));
					last = index;
					position++;
				} else {
					position += 2;
				}
				start = -1;
			}
		}
		sb.append(str.substring(last + 1, chars.length));
		return sb.toString();
	}

	/**
	 * 设置创意默认关键词
	 * 
	 * @param value
	 */
	public void setKeyWord(String value) {
		this.title = this.setKeyWord(this.title, value);
		this.desc1 = this.setKeyWord(this.desc1, value);
		this.desc2 = this.setKeyWord(this.desc2, value);
	}

	public String setKeyWord(String str, String value) {
		StringBuilder sb = new StringBuilder();
		char[] chars = str.toCharArray();
		// 符号{开始处，start为-1时说明尚未找到{
		int start = -1;
		// 上一次}的index
		int last = -1;
		for (int index = 0; index < chars.length; index++) {
			if (chars[index] == '{' && start == -1) {
				start = index;
			}
			if (chars[index] == '}' && start != -1) {
				if (start != index - 1) {
					// String value = str.substring(start + 1, index);
					// Word word = new Word();
					// word.setName(value);
					// this.words.put(start + addIndex, word);
					sb.append(str.substring(last + 1, start));
					sb.append("{" + value + "}");
					last = index;
				}
				start = -1;
			}
		}
		sb.append(str.substring(last + 1, chars.length));
		return sb.toString();
	}

	public void rollbackContent() {

	}

	/**
	 * 获取创意默认关键词列表，按创意中原位置排序
	 * 
	 * @return
	 */
	public List<Word> getWordsAsList() {
		this.generateKeyWord();
		List<Integer> positions = new ArrayList<Integer>(words.keySet());
		Collections.sort(positions);
		List<Word> wordList = new ArrayList<Word>(this.words.size());
		for (Integer position : positions) {
			wordList.add(words.get(position));
		}
		return wordList;
	}

	public String getClassfiedAndDisireStr() {
		StringBuilder sb = new StringBuilder();
		return sb.append(this.industry2Name).append("_")
				.append(this.getRequireType()).append("_")
				.append(this.wordType).append("_").append(this.desireType)
				.toString();
	}
	
	public String toString() {
		return ToStringBuilder.reflectionToString(this);
	}
}
